package io.github.kouleen.manager.listener.player;

import io.github.kouleen.manager.ManagerX;
import io.github.kouleen.minecraft.core.lang.annotation.MinecraftPluginAutoInject;
import io.github.kouleen.minecraft.core.lang.annotation.MinecraftPluginListener;
import io.github.kouleen.minecraft.core.utils.ObjectUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;

/**
 * 玩家尝试登录服务器的事件
 * @author zhangqing
 * @since 2025/2/26 10:49
 */
@MinecraftPluginListener
public class AsyncPlayerPreLoginListener implements Listener {

    @MinecraftPluginAutoInject
    private ManagerX managerX;

    @EventHandler(priority = EventPriority.LOWEST)
    public void onAsyncPlayerPreLoginListenerLowest(AsyncPlayerPreLoginEvent asyncPlayerPreLoginEvent){
        managerX.getLogger().info("AsyncPlayerPreLoginListener onAsyncPlayerPreLoginListenerLowest");
        if (AsyncPlayerPreLoginEvent.Result.ALLOWED != asyncPlayerPreLoginEvent.getLoginResult()) {
            return;
        }
        String name = asyncPlayerPreLoginEvent.getName();
        if(ObjectUtils.isEmpty(asyncPlayerPreLoginEvent.getAddress())){
            asyncPlayerPreLoginEvent.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER,"Error retrieving message");
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onAsyncPlayerPreLoginEventHighest(AsyncPlayerPreLoginEvent asyncPlayerPreLoginEvent){
        managerX.getLogger().info("AsyncPlayerPreLoginListener onAsyncPlayerPreLoginEventHighest");
        if (AsyncPlayerPreLoginEvent.Result.ALLOWED != asyncPlayerPreLoginEvent.getLoginResult()) {
            return;
        }
    }
}
